package clusteringTest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.util.Bytes;

public class LookUpTitleFromURL {

	/**
	 * @param args
	 */
	

	public static Configuration configuration;
	static {
		configuration = HBaseConfiguration.create();
		//configuration.set("hbase.zookeeper.property.clientPort", "2181");
		//configuration.set("hbase.zookeeper.quorum", "192.168.1.100");
		//configuration.set("hbase.master", "192.168.1.100:600000");
	}
	
	
	public String  QueryTitleByURL(String tableName,String url) throws IOException {

			String title ="";
			HTablePool pool = new HTablePool(configuration, 1000);
			HTable table = (HTable) pool.getTable(tableName);


			Filter filter = new SingleColumnValueFilter(Bytes
					.toBytes("url"), null, CompareOp.EQUAL, Bytes
					.toBytes(url));

			Scan scan = new Scan();
			scan.setFilter(filter);
			ResultScanner rs = table.getScanner(scan);
			for (Result r : rs) {
		
				System.out.println("获得到rowkey:" + new String(r.getRow()));
				for (KeyValue keyValue : r.raw()) {
					if ("title".equals(Bytes.toString(keyValue.getFamily())))
				       {
						    title += Bytes.toString(keyValue.getValue());  
					    	System.out.println("title: "+ title);
					    }//if
//					System.out.println("列：" + new String(keyValue.getFamily())
//							+ "====值:" + new String(keyValue.getValue()));
				}//for
			}//for
			rs.close();
			return title;

	}
	
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		LookUpTitleFromURL lt = new LookUpTitleFromURL();
		String result = lt.QueryTitleByURL("blog_page", "bbb0");
		System.out.println("title:  "+result);
	}

}
